<?php
/**
 * 小说Dao
 */

class Dao_NovelModel extends Dao_BaseModel {

    protected $_table = 'novel';

    const NORMAL_STATUS = 1;
    const INFO_KEYWORD = 'NOVEL:NOVEL_INFO_KEY_';
    const EXTIME        = '86400';

    public function __construct ()
    {
        parent::__construct ();
    }
    
    /**
     * 获取小说详情
     */
    public function getNovelInfo($wheres = [])
    {
        if(!isset( $wheres['novel_id'] )){
            return false;
        }
        $where   = array ();
        $where[] = array ( 'novel_status' , self::NORMAL_STATUS );
        $where[] = array ( 'novel_id' , $wheres['novel_id'] );
        $redis   = RedisTool::getInstance();
//        $novel   = $redis->get( self::INFO_KEYWORD . $wheres['novel_id'] );
//        if( $novel ){
//            $info    = json_decode( $novel , true );
//        }
//        if( empty( $info ) ){
            $info = parent::getOne( array ( 'novel_id' , 'novel_author' , 'novel_title' , 'novel_type' ,'novel_main_category', 'novel_category' , 'novel_cover' , 'novel_introduction' , 'novel_pinyin' , 'novel_chapter' , 'novel_charge_id' ) , $where );
            $redis->set( self::INFO_KEYWORD . $wheres['novel_id'] , json_encode( $info ) );
//        }
        return $info;
    }

    /**
     * 获取小说列表.
     * @param $wheres
     * @return array|bool
     */
    public function getNovelList ($wheres = [] , $type = '')
    {
        $where    = array ();
        if( $wheres['novelids'] ){
            $where[] = $wheres['novelids'];
        }
        if( $type == '' ){
            $where[]  = array ( 'novel_status' , self::NORMAL_STATUS );
        }
        $info = parent::getRows ( array ( 'novel_id' , 'novel_author' , 'novel_title' , 'novel_type' , 'novel_category' , 'novel_cover' , 'novel_introduction' ) , $where );
        return $info;
    }
    
    /**
     * 获取新书列表
     */
    public function getNewBookList( $params = [] , $d = 0 )
    {
        $where   = array();
        
        if( $d ){
            $times   = $this->getTimeBetween($d);
            $where[] = array( 'novel_op_time' , '>=' , $times['start'] );
            $where[] = array( 'novel_op_time' , '<=' , $times['end'] );
        }
        
        //关键字
        if( isset( $params['kw'] ) && $params['kw'] != '' ){
            $where[] = array( 'novel_title' , 'like' , '%'.$params['kw'].'%' );
        }
        
        //类型
        if( isset( $params['category'] ) && $params['category'] > 0 ){
            $where[] = array( 'novel_category' , $params['category'] );
        }
        
        //状态
        if( isset( $params['isFinish'] ) && in_array( $params['isFinish'] , [1,2] ) ){
            $where[] = array( 'novel_type' , $params['isFinish'] );
        }
        
        //精选
        if( isset( $params['well_chosen'] ) && $params['well_chosen'] == '1' ){
            $where[] = array( 'novel_well_chosen' , 1 );
        }
        
        //字数
        if( isset( $params['wordCount'] ) ){
            switch( $params['wordCount'] ){
                case 1:
                    $where[] = array( 'novel_word_total' , '<' , 300000 );
                    break;
                case 2:
                    $where[] = array( 'novel_word_total' , '>=' , 300000 );
                    $where[] = array( 'novel_word_total' , '<' , 500000 );
                    break;
                case 3:
                    $where[] = array( 'novel_word_total' , '>=' , 500000 );
                    $where[] = array( 'novel_word_total' , '<' , 1000000 );
                    break;
                case 4:
                    $where[] = array( 'novel_word_total' , '>=' , 1000000 );
                    $where[] = array( 'novel_word_total' , '<' , 2000000 );
                    break;
                case 5:
                    $where[] = array( 'novel_word_total' , '>=' , 2000000 );
                    break;
                default :
                    break;
            }
        }
        //收费状态
        if( isset( $params['needPay'] ) && $params['needPay'] != 999 ){
            if( $params['needPay'] == 1 ){
                $where[] = array( 'novel_charge_id' , 0 );
            }else{
                $where[] = array( 'novel_charge_id' , '>' , 0 );
            }
        }
        
        $orderBy = array();
        if( isset( $params['orderBy'] ) ){
            switch($params['orderBy']){
                case 0:
                    $orderBy = array( 'novel_op_time' => 'desc' , 'novel_click' => 'desc' );
                    break;
                case 1:
                    $orderBy = array( 'novel_click' => 'desc' );
                    break;
                case 2:
                    $orderBy = array( 'novel_op_time' => 'desc' );
                    $where[] = array( 'novel_type' , 2 );
                    break;
                case 3:
                    $orderBy = array( 'novel_op_time' => 'desc' );
                    $where[] = array( 'novel_type' , 1 );
                    break;
                case 4:
                    $orderBy = array( 'novel_click' => 'desc' , 'novel_op_time' => 'desc' );
                    break;
                default :
                    break;
            }
        }
        
        $where[] = array( 'novel_op_status' , 1 );
        $where[] = array( 'novel_status' , 1 );
        $limit  = $params['pageSize'];
        $offset = ( $params['page'] - 1 ) * $limit;
        $fields = array( 'novel_id,novel_author,novel_title,novel_introduction,novel_cover,novel_category,novel_type' );
        $res = parent::getList( $offset , $limit , true , $where , $orderBy , $fields);
//        $res['page'] = $params['page'];
//        if( $res['total'] > $params['page'] * $params['pageSize'] ){
//            $res['page'] = $params['page']+1;
//        }
        return $res;
    }
    
    /**
     * 点击榜
     */
    public function getClickBookList( $page = 1 , $pageSize = 10 , $d = 1 )
    {
        $offset = ( $page - 1 ) * $pageSize;
        $where   = array();
        $where[] = array( 'novel_op_status' , 1 );
        $where[] = array( 'novel_status' , 1 );
        $times   = $this->getTimeBetween($d);
        $where[] = array( 'novel_op_time' , '>=' , $times['start'] );
        $where[] = array( 'novel_op_time' , '<=' , $times['end'] );
        $orderBy = array( 'novel_click' => 'desc' , 'novel_op_time' => 'desc' );
        $fields = array( 'novel_id,novel_author,novel_title,novel_introduction,novel_cover,novel_category,novel_type' );
        $res = parent::getList( $offset , $pageSize , true , $where , $orderBy , $fields);
        return $res;
    }
    
    /**
     * 获取连载完本排行
     */
    public function getTypeBookList( $page = 1 , $pageSize = 10 , $type = 1 , $d = 1 )
    {
        $offset = ( $page - 1 ) * $pageSize;
        $where = array();
        $where[] = array( 'novel_type' , $type );
        $where[] = array( 'novel_op_status' , 1 );
        $where[] = array( 'novel_status' , 1 );
        $times   = $this->getTimeBetween($d);
        $where[] = array( 'novel_op_time' , '>=' , $times['start'] );
        $where[] = array( 'novel_op_time' , '<=' , $times['end'] );
        $orderBy = array( 'novel_click' => 'desc' , 'novel_op_time' => 'desc' );
        $fields  = array( 'novel_id,novel_author,novel_title,novel_introduction,novel_cover,novel_category,novel_type' );
        $res = parent::getList( $offset , $pageSize , true , $where , $orderBy , $fields);
        return $res;
    }
    
    /**
     * 获取时间区间
     */
    private function getTimeBetween( $type )
    {
        $now        = date( 'Y-m-d H:i:s' );
        $year       = date('Y');
        $month_n    = date('n');
        $day_j      = date('j');
        $week_day   = date('w');
        switch( $type ){
            case 1:  //日榜
                $yestoday = date( 'Y-m-d 00:00:00' , strtotime('-1 days') );
                $tomorrow = date( 'Y-m-d 00:00:00' , strtotime('+1 days') );
                $times['start'] = $yestoday;
                $times['end'] = $tomorrow;
                break;
            case 2:  //周榜
                if( $week_day == 0 || $week_day == 1 ){
                    $times['start'] = date( 'Y-m-d 00:00:00' , strtotime('-7 days') );
                    $times['end'] = $now;
                }else{
                    $times['start'] = date( 'Y-m-d 00:00:00' , time() - ( $week_day - 1 ) * 24 * 3600 );
                    $times['end'] = $now;
                }
                break;
            case 3:  //月榜
                if( $day_j == 1 ){
                    if( $month_n == 1 ){
                        $last_year = $year - 1;
                        $times['start'] = $last_year.'-12-01 00:00:00';
                        $times['end'] = $now;
                    }else{
                        $last_month = date( 'Y-m-01 00:00:00' , strtotime('-1 month') );
                        $times['start'] = $last_month;
                        $times['end'] = $now;
                    }
                }else{
                    $times['start'] = date( 'Y-m-01 00:00:00' );
                    $times['end'] = $now;
                }
                break;
            default :
                break;
        }
        return $times;
    }
    
}
